<?php

	/**
	 * 库存类模型
	 * Auther：QiuXiangCheng
	 * Date：2016/07/18
	 */
	class Base_Model_Stock extends Model_Base{

		/**
		 * 分页数
		 * @var integer
		 */
		public $pageAmount = 0;
		/**
		 * 获取子仓库存
		 * @param array $params
		 */
		public function getSonStock(array $params) {
			$catelogModel = new Application_Model_ProductCatalog();
			$where = $init = ' where 1 ';
			$filter = [];
			if ($params['warehouse_id'] != '') {
				$where .= ' and pws.warehouse_code = :wcode';
				$filter['wcode'] = $params['warehouse_id'];
			}
			if ($params['select_cat_id'] != '') {
				$categorys = array_column($catelogModel->getAllChildCatalog($params['select_cat_id'],['id']), 'id');
				$where .= $this->db()->quoteInto(' and pc.id in (?)', $categorys);
			}
			if ($params['life_cycle'] != '') {
				$where .= ' and pws.life_cycle = :cycle';
				$filter['cycle'] = $params['life_cycle'];
			}
			if ($params['search_type'] != '' && $params['searchName'] != '') {
				if ($params['search_type'] == 1) {
					$where .= $this->db()->quoteInto(" and pws.goods_sn in (?)", explode(',', $params['searchName']));
				} else {
					$where .= ' and p.product_name like :product_name';
					$filter['product_name'] = '%'.$params['searchName'].'%';
				}
			}
			$sql = "select %field% from p_product_warehouse_stock pws 
					left join p_product p using (goods_sn) 
					left join p_product_catalog pc on pc.id=p.catalog_id 
					left join c_warehouse w on w.warehouse_code = pws.warehouse_code 
					left join p_product_sales_statistics pss on pss.goods_sn=pws.goods_sn and pss.warehouse_code=pws.warehouse_code";
			$field = "pws.*,p.product_name,p.thumb_url,pc.name_ch,w.warehouse_name,
					pss.real_sale_7,pss.real_sale_15,pss.real_sale_30,pss.sale_7,pss.sale_15,pss.sale_30";
			
			$query_sql = str_replace('%field%', $field, $sql);
			$query_sql .= $where . " order by pws.id limit ". ($params['page']-1) * $params['perPage'] .",".$params['perPage'];
			$countSql = str_replace('%field%', 'count(1) as total', $sql) . $where;
			
			$data = [];
			if ($where != $init) {
				$data['list'] = $this->db()->fetchAll($query_sql, $filter);
				$data['count'] = $this->db()->fetchOne($countSql, $filter);
			} else {
				$data['list'] = [];
				$data['count'] = 0;
			}
			return $data;
		}
		/**
		 * 获取总仓库存
		 * @param array $params
		 */
		public function getSumStock(array $params) {
			$catelogModel = new Application_Model_ProductCatalog();
			$where = $init = ' where 1 ';
			$filter = [];
			if ($params['select_cat_id'] != '') {
				$categorys = array_column($catelogModel->getAllChildCatalog($params['select_cat_id'],['id']), 'id');
				$where .= $this->db()->quoteInto(' and pc.id in (?)', $categorys);
			}
			if ($params['search_type'] != '' && $params['searchName'] != '') {
				if ($params['search_type'] == 1) {
					$where .= $this->db()->quoteInto(" and pws.goods_sn in (?)", explode(',', $params['searchName']));
				} else {
					$where .= ' and p.product_name like :product_name';
					$filter['product_name'] = '%'.$params['searchName'].'%';
				}
			}
			$sql = "select %field% from p_product_warehouse_stock pws 
					left join p_product p using (goods_sn) 
					left join p_product_catalog pc on pc.id=p.catalog_id 
					left join c_warehouse w on w.warehouse_code = pws.warehouse_code
					left join p_product_sales_statistics pss on pss.goods_sn=pws.goods_sn and pss.warehouse_code=pws.warehouse_code";
			
			$field = "p.thumb_url,pws.goods_sn,p.product_name,pc.name_ch,
					sum(if((w.type ='4' || w.type='5'),0,pws.base_stock)) as base_stock,
					sum(if((w.type ='4' || w.type='5'),0,pws.stock_order_nums)) as stock_order_nums,
					sum(if((w.type ='4' || w.type='5'),0,pws.stock_wait_for_send)) as stock_wait_for_send,
					sum(if((w.type ='4' || w.type='5'),0,pws.frozen_stock)) as frozen_stock,
					sum(if((w.type ='4' || w.type='5'),0,pws.rejects_stock)) as rejects_stock,
					sum(if((w.type ='4' || w.type='5'),0,pws.stock_on_way)) as stock_on_way,
					sum(if((w.type ='4' || w.type='5'),0,pws.stock_can_allot)) as stock_can_allot,
					sum(if((w.type ='4' || w.type='5'),0,real_sale_7)) as real_sale_7,
					sum(if((w.type ='4' || w.type='5'),0,real_sale_15)) as real_sale_15,
					sum(if((w.type ='4' || w.type='5'),0,real_sale_30)) as real_sale_30";
		
			$query_sql = str_replace('%field%', $field, $sql);
			$query_sql .= $where. " group by pws.goods_sn order by pws.id limit ". ($params['page']-1) * $params['perPage'] .",".$params['perPage'];
			$countSql = str_replace('%field%', 'count(*) as total', $sql) . $where ." group by pws.goods_sn";
			$countSql = "select count(*) as total from (".$countSql.") as temps";
			
			$data = [];
			if ($where != $init) {
				$data['list'] = $this->db()->fetchAll($query_sql, $filter);
				$data['count'] = $this->db()->fetchOne($countSql, $filter);
			} else {
				$data['list'] = [];
				$data['count'] = 0;
			}
			return $data;
		}
	}